package com.pai4j.jianlipai.service;
import com.pai4j.domain.vo.response.UserBaseResponseInfoVO;
import com.pai4j.remote.user.UserServiceClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 *   角色服务
 *
 * @author: CYM-pai
 * @date: 2025/05/05 14:08
 **/
@Slf4j
@Service
public class RoleService {
    @Resource
    private UserServiceClient userServiceClient;

    /**
     * 校验用户是否拥有权限
     *
     * @param userId   用户ID
     * @param roleCode 权限CODE
     * @return
     */
    public boolean hasRole(String userId, Integer roleCode) {
        if (StringUtils.isBlank(userId) || roleCode == null) {
            log.error("{} not has role: {}!", userId, roleCode);
            return false;
        }
        UserBaseResponseInfoVO userBaseResponseInfoVO = userServiceClient.getUserBaseInfo(userId).getData();
        Assert.isTrue(userBaseResponseInfoVO != null, "用户不存在");
        return roleCode.equals(userBaseResponseInfoVO.getRoleCode());
    }
}
